package cn.codeforfun.gitee.utils;

import java.lang.reflect.Field;
import java.util.HashMap;

/**
 * 反射工具类
 *
 * @author wangbin
 */
public class ReflectionUtils {

    public static HashMap<String, Object> getFieldsMap(Object object) {
        Field[] fields = object.getClass().getDeclaredFields();
        HashMap<String, Object> result = new HashMap<>(fields.length);
        try {
            for (Field field : fields) {
                if (!field.isAccessible()) {
                    field.setAccessible(true);
                }
                String fieldName = field.getName();
                Object fieldValue = field.get(object);
                if (null != fieldValue) {
                    result.put(fieldName, fieldValue);
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return result;
    }
}